package demo;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.aerospike.client.BatchRead;
import com.aerospike.client.Bin;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.policy.BatchPolicy;

public class BatchGetTest {

	public static void main(String[] args) throws Exception {

		IAerospikeClient client = AsClientUtil.getClient();
		System.out.println("client=" + client);

		String namespace = "ns1";
		String dataSetName = "testset";
		final int NUM = 7;
		
		for(int i=0;i<NUM;i++) {
		    String keyStr = buildKeyString(i);
		    Key key = new Key(namespace,dataSetName,keyStr);
		    
		    Bin bin1 = buildBin(keyStr,"bin1");
	        Bin bin2 = buildBin(keyStr,"bin2");

	        client.put(null, key,bin1,bin2);
		}
		
       
		for(int i=0;i<NUM;i++) {
            String keyStr = buildKeyString(i);
            Key key = new Key(namespace,dataSetName,keyStr);
           

            Record record = client.get(null, key);
            
            System.out.println(record);
        }
		///
		BatchPolicy bp = new BatchPolicy();
		List<BatchRead> list = new ArrayList<BatchRead>();
		for(int i=0;i<NUM;i++) {
		    String keyStr = buildKeyString(i);
            Key key = new Key(namespace,dataSetName,keyStr);
           
		    BatchRead br = new BatchRead(key,new String[] {"bin1"});
		    list.add(br);
		    
		}
		client.get(bp, list);
		
		for(BatchRead item:list) {
		    System.out.println(item+","+item.record);
		}
		
		client.close();

	}
	
	private static String buildKeyString(int index) {
	    String key =  "batch_get_test_key_"+index;
	    return key;
	}
	
	private static Bin buildBin(String key,String name,String value) {
        return new Bin(name,key+"_"+name+"_"+value);
    }
	
	private static Bin buildBin(String key,String name) {
        return buildBin(key,name,LocalDateTime.now().toString());
    }

}
